package paa.crawlers.delicious;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserExtractor {

	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	
	public DeliciousUser extractUser(String id) throws Exception {
		System.out.println(id);
		
		DeliciousUser user = new DeliciousUser(id);
		String urlBase = "http://del.icio.us/";
		
		extractUrls(id, user, urlBase);
		Thread.sleep(10000);
		extractNetwork(id, user, urlBase);
		
		return user;
	}

	private void extractNetwork(String id, DeliciousUser user, String urlBase) throws Exception {
		BufferedReader in = null;
		try {
			URL delicious = new URL(urlBase + "network/" + id);
			URLConnection dc = delicious.openConnection();
			
			in = new BufferedReader(new InputStreamReader(dc.getInputStream()));
			String inputLine;

			//Fan pattern:
			//<li class="fanbi">
            //<a class="uname" href="/guiga">guiga</a>
			Pattern fanPattern = Pattern.compile("\\s*<li class=\"fanbi\\s*\"\\s*>");
			
			//Net:
			//<li class="netbi ">
            //<a class="uname" href="/guiga">guiga</a>                    <a href="/network/flaviovdf?networkrm=guiga;key=7243849e7c273074150f847649dc3289" class="unsubscribe" title="remove" onclick="makeConfirm(this);return false">x</a>
			Pattern netPattern = Pattern.compile("\\s*<li class=\"netbi\\s*\"\\s*>");
			
			//Username pattern
			Pattern userPattern = Pattern.compile("(\\s*<a class=\"uname\" href=\"/)(.*?)(\">).*");
			
			boolean isFan = false;
			while ((inputLine = in.readLine()) != null)	{
				Matcher fanMatcher = fanPattern.matcher(inputLine);
				Matcher netMatcher = netPattern.matcher(inputLine);
				
				if (fanMatcher.matches()) {
					isFan = true;
				} else if (netMatcher.matches()) {
					isFan = false;
				}
				
				Matcher matcher = userPattern.matcher(inputLine);
				if (matcher.matches()) {
					String group = matcher.group(2);
					DeliciousUser related = new DeliciousUser(group);
					
					if (isFan) {
						user.addFanUser(related);
					} else {
						user.addNetworkUser(related);
					}
				}
			}
			
			in.close();
		} finally {
			if (in != null)  in.close();
		}
	}

	private void extractUrls(String id, DeliciousUser user, String urlBase)	throws MalformedURLException, IOException {
		BufferedReader in = null;
		try {
			int page = 1;
			boolean collect=true;
			
			while(collect) {
				URL delicious = new URL(urlBase + id + "?page="+page++);
				URLConnection dc = delicious.openConnection();
				
				in = new BufferedReader(new InputStreamReader(dc.getInputStream()));
				String inputLine;
				
				// <li class="post" key="872ehewqkjdho879y32oeh">
				Pattern urlPattern = Pattern.compile("(\\s*<li class=\"post.*\"?key=\")(.*)(\">.*)");
				
				// <div class="meta">to <a class="tag" href="/Catho_Online/jornal">jornal</a> <a class="tag" href="/Catho_Online/Jornal_de_Uberaba">Jornal_de_Uberaba</a> <a class="tag" href="/Catho_Online/13_maio">13_maio</a> <a class="tag" href="/Catho_Online/maio_2008">maio_2008</a> <a class="tag" href="/Catho_Online/2008">2008</a> <a class="tag" href="/Catho_Online/pesquisa">pesquisa</a> <a class="tag" href="/Catho_Online/emprego">emprego</a> <span class="date" title="2008-05-16T15:05:27Z">... 1 hour ago</span> </div>
				Pattern tagsPattern = Pattern.compile("(.*<div class=\"meta\">to\\s)(.*?)<span class=\"date\" title=\"(.*?)\">.*");
				
				//<h3>No items</h3>
				Pattern noItemsPattern = Pattern.compile("<h3>No\\sitems</h3>");
				
				DeliciousURL url2tags = null;
				while ((inputLine = in.readLine()) != null)	{
					Matcher tagsMatcher = tagsPattern.matcher(inputLine);
					Matcher urlMatcher = urlPattern.matcher(inputLine);
					Matcher noItemsMatcher = noItemsPattern.matcher(inputLine);
					
					if (noItemsMatcher.matches()) {
						collect = false; 
						break;
					}
					
					if (urlMatcher.matches()) {
						String url = urlMatcher.group(2);
						
						url2tags = new DeliciousURL(url);
						user.addUrl(url2tags);
						
					}
					
					if (tagsMatcher.matches()) {
						//to <a class="tag" href="/pruffini/media">media</a> <a class="tag" href="/pruffini/blogging">blogging</a> ... <a class="pop" href="/url/2267ea88843898f27d6937e3be3812b8" style="padding: 0 0.2em; background-color: rgb(100%, 100%, 100%);">saved by 1 other person</a> <span class="date" title="2008-04-06T04:58:19Z">... on april 06</span> </div>
						String tags = tagsMatcher.group(2);
						String[] split = tags.split(">");
						for (String token : split) {
							if (token.endsWith("</a") && !token.contains(" ")) {
								String tag = token.replaceAll("</a", "");
								url2tags.addTag(tag);
							}
						}
						
						try {
							Date urlDate = sdf.parse(tagsMatcher.group(3));
							url2tags.setDate(urlDate);
						} catch (ParseException e) {
							System.err.println("Error occurred: " + e);
						}
					}
				}
				
				in.close();
			}
		} finally {
			if (in != null)  in.close();
		}
	}
}
